package stef.home.arp.labos1.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import stef.home.arp.labos1.matrix.Matrix;

public class Utils {

	public static Matrix createMatrix(String input){
		BufferedReader reader = null;
		List<Double> elements = new ArrayList<Double>();
		Matrix matrix = null;
		int nrows = 0, ncolumns = 0;
		try {
			reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(new FileInputStream(input))));
		} catch (FileNotFoundException e) {
			System.out.println("file "+input+" not found!");
			return null;
		}
		try {
			String line;
			String[] separatedLine;
			while((line = reader.readLine())!=null){
				nrows++;
				separatedLine = line.split(" ");
				ncolumns = separatedLine.length;
				for(String s : separatedLine){
					elements.add(Double.valueOf(s));
				}
			}
		} catch (IOException e) {
			System.out.println("file "+input+" could not be read!");
			return null;
		} finally { 
			try { reader.close(); } catch (IOException ignorable) {}}
		
		matrix = new Matrix(nrows, ncolumns, elements);
		return matrix;
	}
	
	public static void writeMatrix(Matrix A,String output){
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(output))));
		} catch (FileNotFoundException e) {
			System.out.println("File "+output+" not found");
			return;
		}
		try{
			DecimalFormat df = new DecimalFormat("0.0000");
			for(int i = 0; i<A.getNcolumns();i++){
				for(int j = 0; j<A.getNrows(); j++){
					writer.write(df.format(A.getMatrix()[i][j])+"\t");
				}
				writer.newLine();
			}
		}catch (IOException e) {
			System.out.println("Cannot write in "+output+" file");
			return;
		}finally{ try { writer.close(); } catch (Exception ignorable) {}
		}
	}
	public static Matrix createIdentityMatrix(int n){
		double[][] I = new double[n][n];
		for(int i=0;i<n;i++)
			I[i][i] = 1;
		return new Matrix(n, n, I);
	}
	
	public static void printMatrix(Matrix matrix){
		DecimalFormat df = new DecimalFormat(" 00.0000;-00.0000");
		for(int i = 0; i < matrix.getNrows();i++){
			for(int j=0; j < matrix.getNcolumns(); j++){
				if(matrix.getMatrix()[i][j] == -0)
					matrix.setElement(0, i, j);
				System.out.print(df.format(matrix.getMatrix()[i][j])+" ");
			}
			System.out.println();
		}
		System.out.println();
		
	}
	
	public static Matrix copyMatrix(Matrix A){
		double[][] B = new double[A.getNrows()][A.getNcolumns()];
		for(int i = 0; i < A.getNrows(); i++)
			for(int j = 0; j<A.getNcolumns(); j++)
				B[i][j] = A.getMatrix()[i][j];
		return new Matrix(A.getNrows(),A.getNcolumns(),B);
	}
}
